package com.start.oss;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.start.common.tools.SpringContextTools;
import com.start.oss.storage.LocalOssService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.time.LocalDate;

@Data
@Configuration
@ConfigurationProperties(prefix = "start.upload.storages.local")
public class StartOssLocalStorageConfig {
    public static final String DIRECTORY_PUBLIC = "public";
    public static final String DIRECTORY_PRIVATE = "private";
    private String rootPath = "upload";
    private String rootUrl = "upload";
    private boolean enabledDate = true;
    private boolean isEnable = true;
    private static final String privateRootUrl = "/start/resource/";
    private String dateFormat = "YYYYMM";
    private String publicDirectory = DIRECTORY_PUBLIC;
    private String privateDirectory = DIRECTORY_PRIVATE;

    public String getRootUrl() {
        return rootUrl.replaceAll("/{2,}", "/");
    }

    public String getRootPath() {
        return rootPath.replace("/", File.separator).replaceAll("/{2,}", File.separator);
    }

    public String getPublicRootUrl() {
        StringBuilder urlSb = new StringBuilder(rootUrl.replace("*", ""));
        urlSb.append("/");
        urlSb.append(LocalOssService.OSS_TYPE);
        urlSb.append("/");
        urlSb.append(StringUtils.isNoneEmpty(publicDirectory) ? publicDirectory : DIRECTORY_PUBLIC);
        urlSb.append("/");
        return urlSb.toString().replaceAll("/{2,}", "/");
    }

    public String getPublicRootDirectoryPath() {
        StringBuilder pathSb = new StringBuilder(rootPath.replace("*", ""));
        pathSb.append(File.separator);
        pathSb.append(LocalOssService.OSS_TYPE);
        pathSb.append(File.separator);
        pathSb.append(StringUtils.isNoneEmpty(publicDirectory) ? publicDirectory : DIRECTORY_PUBLIC);
        pathSb.append(File.separator);
        String path = pathSb.toString().replace("/", File.separator).replace("\\", File.separator);
        File filePath = new File(path);
        if (filePath.isFile() || filePath.exists()) {
            filePath.mkdirs();
        }
        return filePath.getAbsolutePath();
    }

    public String getPublicUrl() {
        StringBuilder urlSb = new StringBuilder("/");
        urlSb.append(SpringContextTools.getContextPath().replace(".", "/"));
        urlSb.append("/");
        urlSb.append(rootUrl.replace("*", ""));
        urlSb.append("/");
        urlSb.append(LocalOssService.OSS_TYPE);
        urlSb.append("/");
        urlSb.append(StringUtils.isNoneEmpty(publicDirectory) ? publicDirectory : DIRECTORY_PUBLIC);
        urlSb.append("/");
        if (isEnabledDate()) {
            final String format = LocalDateTimeUtil.format(LocalDate.now(), dateFormat);
            urlSb.append(format);
        }
        urlSb.append("/");
        return urlSb.toString().replaceAll("/{2,}", "/");
    }

    public String getPrivateUrl() {
        StringBuilder urlSb = new StringBuilder("/");
        urlSb.append(SpringContextTools.getContextPath().replace(".", "/"));
        urlSb.append("/");
        urlSb.append(privateRootUrl);
        urlSb.append("/");
        urlSb.append(LocalOssService.OSS_TYPE);
        urlSb.append("/");
        urlSb.append(StringUtils.isNoneEmpty(privateDirectory) ? privateDirectory : DIRECTORY_PRIVATE);
        urlSb.append("/");
        if (isEnabledDate()) {
            final String format = LocalDateTimeUtil.format(LocalDate.now(), dateFormat);
            urlSb.append(format);
        }
        urlSb.append("/");
        return urlSb.toString().replaceAll("/{2,}", "/");
    }

    public String getPublicDirectoryPath() {
        StringBuilder path = new StringBuilder(getPublicRootDirectoryPath());
        path.append(File.separator);
        if (isEnabledDate()) {
            final String format = LocalDateTimeUtil.format(LocalDate.now(), dateFormat);
            path.append(format);
        }
        path.append(File.separator);
        return path.toString().replaceAll(File.separator + "{2,}", File.separator);
    }

    public String getPrivateDirectoryPath() {
        StringBuilder path = new StringBuilder(rootPath.replace("/", File.separator).replace("\\", File.separator));
        path.append(File.separator);
        path.append(LocalOssService.OSS_TYPE);
        path.append(File.separator);
        path.append(StringUtils.isNoneEmpty(privateDirectory) ? privateDirectory : DIRECTORY_PRIVATE);
        path.append(File.separator);
        if (isEnabledDate()) {
            final String format = LocalDateTimeUtil.format(LocalDate.now(), dateFormat);
            path.append(format);
        }
        path.append(File.separator);
        return path.toString().replaceAll(File.separator + "{2,}", File.separator);
    }

    public static void main(String[] args) {
        final StartOssLocalStorageConfig startOssLocalStorageConfig = new StartOssLocalStorageConfig();
        System.out.println("root:url:" + startOssLocalStorageConfig.getRootUrl());
        System.out.println("root:file:" + startOssLocalStorageConfig.getRootPath());
        System.out.println("public:file:" + startOssLocalStorageConfig.getRootPath());
        System.out.println("PublicRootUrl:url:" + startOssLocalStorageConfig.getPublicRootUrl());
        System.out.println("PublicRootDirectoryPath:file:" + startOssLocalStorageConfig.getPublicRootDirectoryPath());
        System.out.println("publicUrl:" + startOssLocalStorageConfig.getPublicUrl());
        System.out.println("publicFile:" + startOssLocalStorageConfig.getPublicDirectoryPath());
        System.out.println("privateUrl:" + startOssLocalStorageConfig.getPrivateUrl());
        System.out.println("privateFile:" + startOssLocalStorageConfig.getPrivateDirectoryPath());

    }


}
